package com.jxsd.edu.cn.struts2.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.jxsd.edu.cn.struts2.models.Category;
import com.jxsd.edu.cn.struts2.models.LabelView;
import com.jxsd.edu.cn.struts2.models.Pmsgstoreview;
import com.jxsd.edu.cn.struts2.models.PubMsgview;
import com.jxsd.edu.cn.struts2.models.SmallCatagory;
import com.jxsd.edu.cn.struts2.models.Topic;
import com.jxsd.edu.cn.struts2.models.UserInfo;
import com.jxsd.edu.cn.struts2.serviceImpl.IndexServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.LabelServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.PubMsgServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.SmallServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.StoreServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.TopicServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.UserServiceImpl;
import com.jxsd.edu.cn.struts2.services.IndexService;
import com.jxsd.edu.cn.struts2.services.LabelService;
import com.jxsd.edu.cn.struts2.services.PubMsgService;
import com.jxsd.edu.cn.struts2.services.SmallService;
import com.jxsd.edu.cn.struts2.services.StoreService;
import com.jxsd.edu.cn.struts2.services.TopicService;
import com.jxsd.edu.cn.struts2.services.UserService;
import com.jxsd.edu.cn.tools.Page;
import com.jxsd.edu.cn.tools.PageBean;
import com.opensymphony.xwork2.ActionContext;


/**
 * 用户的个人空间管理
 * */
public class PersonSpaceAction 
{
	//一级类别信息
 	private     List<Category>   list;
 	//用户信息
 	private   UserInfo  user=new  UserInfo();
 	//个性化推荐信息
 	private   List<PubMsgview>  pubMsgs;
 	//兴趣标签
 	private   List<LabelView>  labels;
 	//所有的标签
 	private   List<SmallCatagory>  smalls;
 	//不在用户兴趣标签里面的物品发布记录
 	private   Page<PubMsgview>   page;
 	
 	
 	//用户已经发布信息记录列表
 	private   Page<PubMsgview>   personPubMsg;
 	//用户已经发布信息记录列表分页请求器
 	private   PageBean<PubMsgview>  pubMsgPagebean=new   PageBean<PubMsgview>(10);
 	
 	//用户已经发布的帖子的列表
 	private   Page<Topic>    personTopic;
 	//用户已经发布的帖子的列表分页请求器
 	private   PageBean<Topic>  topicPageBean=new   PageBean<Topic>();
 	
 	
 	//用户已经收藏了的物品信息列表
 	private   Page<Pmsgstoreview>   pmsgStorePage;
 	//用户已经收藏了的物品信息列表分页请求器
 	private   PageBean<Pmsgstoreview>   pmsgStorePageBean=new  PageBean<Pmsgstoreview>();
 	
 	//用户Service
    private   UserService  userService;
 	//IndexService
    private     IndexService   indexService;
    //PubMsgService
    private   PubMsgService    pubService;
    //LabelService
    private   LabelService  labelService;
    //SmallService
    private   SmallService   smallService;
    //TopicService
    private   TopicService   topicService;
    //StoreService
    private   StoreService   storeService;
    
    //构造方法
    public  PersonSpaceAction()
    {
    	userService=new  UserServiceImpl();
    	pubService=new  PubMsgServiceImpl();
    	indexService=new  IndexServiceImpl();
    	labelService=new  LabelServiceImpl();
    	smallService=new  SmallServiceImpl();
    	topicService=new  TopicServiceImpl();
    	storeService=new  StoreServiceImpl();
    }
    
    /**
     * 获取用户的标签物品信息
     * */
    public   String  queryLabelMsg()
    {
    	queryBasicData();
    	//获取个性化推荐信息列表
    	String[]  smallNames;
    	String  s="";
    	for(LabelView  label:labels)
    	{
    		s+=label.getSmallName();
    		s+=",";
    	}
    	//去掉最后一个逗号
    	s=s.substring(0, s.lastIndexOf(","));
    	smallNames=s.split(",");
    	//用户没有登记兴趣标签
    	if(smallNames.length<1)
    	{
    		return  "error";
    	}
    	pubMsgs=pubService.getLabelPubMsgs(smallNames, user.getCityName(), user.getUniverName());
//    	HttpServletRequest  res
    	
    	return  "tab1";
    }
    /**
     * 获取所有的二级类别信息
     * @throws IOException 
     * */
    public  void  QueryAllSmalls() throws IOException
    {
    	//获取二级类别信息
    	smalls=smallService.getAllSmalls();
 	   
 	    HttpServletResponse response=ServletActionContext.getResponse();
 		response.setContentType("text/plain");
 		response.setCharacterEncoding("UTF-8");
 		
 		String  small="";
 		for(SmallCatagory  s:smalls)
 		{
 			small+=s.getSmallName();
 			small+="/";
 		}
 		small=small.substring(0, small.lastIndexOf("/"));
 		PrintWriter  writer=response.getWriter();

 		writer.write(small);
 		writer.flush();
 		writer.close();
    }
    /**
     * 获取用户已经发布的信息列表
     * */
    public  String  queryPubMsgs()
    {
    	queryBasicData();
    	
    	personPubMsg=pubService.getPersonPubMsgs(Integer.toString(user.getUserId()), pubMsgPagebean);
    	return  "tab3";
    }
    
    /**
     * 获取用户已经发布的帖子的列表
     * */
    public  String  queryPersonTopics()
    {
    	queryBasicData();
    	
    	personTopic=topicService.getPersonTopic(topicPageBean, Integer.toString(user.getUserId()));
    	return  "tab4";
    }
    
    /**
     * 获取用户已经收藏了的列表
     * */
    public   String   queryMsgStore()
    {
    	queryBasicData();
    	
    	pmsgStorePage=storeService.getPersonStores(pmsgStorePageBean, Integer.toString(user.getUserId()));
    	
    	return  "tab5";
    }
    
    /**
     *用户编辑用户信息后返回需要的信息 
     * */
    public  String  queryPersonMsg()
    {
    	queryBasicData();
    	//获取二级类别信息
    	smalls=smallService.getAllSmalls();
    	return  "tab2";
    }
    
    /**
     * 获取页面的基本数据
     * */
    private   void  queryBasicData()
    {
    	//获取导航信息
    	getCatagories();
    	//获取用户信息
    	queryUserInfo();
    	//获取用户标签信息
    	queryLabels();
    	//获取不在用户兴趣标签列表里面的其他同校数据
    	recommendPubMsg();
    }
    
    
    /**
     * 获取导航信息
     * */
    private  void  getCatagories()
    {
    	list=indexService.getAllCatagory();
    }
    
    /**
     * 获取用户信息
     * */
    private  void  queryUserInfo()
    {
    	String  nickName=(String)ActionContext.getContext().getSession().get("nickName");
    	user=userService.getUserInfo("nickName", nickName);
    }
    
   /**
    * 获取用户的兴趣标签
    * */
   private  void  queryLabels()
   {
	   labels=labelService.getLabelViews(Integer.toString(user.getUserId()));
   }
   
   /**
    * 获取不在用户兴趣标签列表里面的其他同校数据
    * @throws IOException 
    * */
   private  void  recommendPubMsg()
   {
	   page=pubService.recommendPubMsg(user.getNickName());
   }
   
	public List<Category> getList() {
		return list;
	}
	
	public void setList(List<Category> list) {
		this.list = list;
	}
	
	public UserInfo getUser() {
		return user;
	}
	
	public void setUser(UserInfo user) {
		this.user = user;
	}
	
	public List<PubMsgview> getPubMsgs() {
		return pubMsgs;
	}
	
	public void setPubMsgs(List<PubMsgview> pubMsgs) {
		this.pubMsgs = pubMsgs;
	}
	
	public List<LabelView> getLabels() {
		return labels;
	}
	
	public void setLabels(List<LabelView> labels) {
		this.labels = labels;
	}

	public List<SmallCatagory> getSmalls() {
		return smalls;
	}

	public void setSmalls(List<SmallCatagory> smalls) {
		this.smalls = smalls;
	}

	public Page<PubMsgview> getPage() {
		return page;
	}

	public void setPage(Page<PubMsgview> page) {
		this.page = page;
	}

	public Page<PubMsgview> getPersonPubMsg() {
		return personPubMsg;
	}

	public void setPersonPubMsg(Page<PubMsgview> personPubMsg) {
		this.personPubMsg = personPubMsg;
	}

	public PageBean<PubMsgview> getPagebean() {
		return pubMsgPagebean;
	}

	public void setPagebean(PageBean<PubMsgview> pubMsgPagebean) {
		this.pubMsgPagebean = pubMsgPagebean;
	}

	public Page<Topic> getPersonTopic() {
		return personTopic;
	}

	public void setPersonTopic(Page<Topic> personTopic) {
		this.personTopic = personTopic;
	}

	public PageBean<Topic> getTopicPageBean() {
		return topicPageBean;
	}

	public void setTopicPageBean(PageBean<Topic> topicPageBean) {
		this.topicPageBean = topicPageBean;
	}

	public Page<Pmsgstoreview> getPmsgStorePage() {
		return pmsgStorePage;
	}

	public void setPmsgStorePage(Page<Pmsgstoreview> pmsgStorePage) {
		this.pmsgStorePage = pmsgStorePage;
	}

	public PageBean<Pmsgstoreview> getPmsgStorePageBean() {
		return pmsgStorePageBean;
	}

	public void setPmsgStorePageBean(PageBean<Pmsgstoreview> pmsgStorePageBean) {
		this.pmsgStorePageBean = pmsgStorePageBean;
	}

}
